package isoTest
{
	import com.friendsofed.isometric.DrawnIsoBox;
	import com.friendsofed.isometric.DrawnIsoTile;
	import com.friendsofed.isometric.IsoUtils;
	import com.friendsofed.isometric.IsoWorld;
	import com.friendsofed.isometric.Point3D;
	import flash.events.Event;
	import iscript.uiFramework.baseShape.Ball;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	[SWF(backgroundColor=0xffffff)]
	public class WorldTest extends Sprite
	{
		private var world:IsoWorld;
		
		private	var _uint:Number = 45
		
		public function get usefulUnit():Number {
			//32
		         return Math.round(_uint * 0.71);
			}
		
		public function WorldTest()
		{
			
			trace(32>>1, 13>>1)
			
		//	stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			world = new IsoWorld();
			
			world.x = 100;
			world.y = 100;
			world.render(this);
			
			//trace(Math.floor(31.95),Math.round(31.95))
			//addChild(world);
			
			
trace(">>>>>",usefulUnit)
			for(var i:int = 0; i < 6; i++)
			{
				for(var j:int = 0; j < 2; j++)
				{
					var tile:DrawnIsoTile = new DrawnIsoTile(usefulUnit, 0xcccccc);
					//tile.opaqueBackground = 0xFF0080;
					tile.position = new Point3D(i * usefulUnit, 0, j * usefulUnit);
					//if (i > 0) tile.color = 0xFF0080;
				/*	tile.label.text = i + "-" + j;
					tile.label.x = -8;
					tile.label.y = -8;*/
					world.addChildToFloor(tile);
					
					var ball:Ball = new Ball(5);
					ball.alpha = 0.5;
					tile.addChild(ball);
					
					trace(IsoUtils.isoToScreen(tile.position))
					
					//trace(tile.position.x, tile.position.y, tile.position.z)
					trace("X:",i,"Y:",j,tile.x, tile.y, tile.width, tile.height);
				}
			}
			
			world.border = true;
		 	stage.addEventListener(MouseEvent.CLICK, onWorldClick);
		}
		private var _yy:int = 0
		private var target:DrawnIsoBox;	
		private var isAdd:Boolean
		private function onWorldClick(event:MouseEvent):void
		{
			if(isAdd)_uint = 45
			var box:DrawnIsoBox = new DrawnIsoBox(usefulUnit,Math.random() * 0xffffff,usefulUnit);
			var posMouse:Point  = new Point(world.mouseX, world.mouseY)
			var pos:Point3D = IsoUtils.screenToIso(posMouse);
		 	trace(posMouse.x,posMouse.y,"<<<<<",pos.x,pos.y,pos.z,">>>>>>>>>>>>")
			
		//	pos.y = _yy++
			trace(pos.x / usefulUnit, Math.round(pos.x / usefulUnit));
			
			if (!isAdd) { 
				
			pos.x = Math.round(pos.x / usefulUnit) * usefulUnit;
				} else {
			pos.x = Math.round(pos.x / usefulUnit) * usefulUnit;
			//-(32 >> 1)
				}
			pos.y = Math.round(pos.y / usefulUnit) * usefulUnit;
			if (isAdd)
			pos.y = Math.round(pos.y / usefulUnit) * usefulUnit-usefulUnit;
			pos.z = Math.round(pos.z / usefulUnit) * usefulUnit;
		/*	if (!isAdd) { 
			pos.z = Math.round(pos.z / usefulUnit) * usefulUnit;
			}else {
			
			pos.z = Math.round(pos.z / usefulUnit) * usefulUnit-(32>>1);
				}*/
			//if(isAdd) pos.x +=0.5
		//	pos.x +=0.5
			//posx +=0.5
			box.position = pos;
			
			
		 	//trace(pos.x,pos.y,pos.z,"====================")

			world.addChildToWorld(box);
			box.alpha = 0.7
				var ball:Ball = new Ball(5,0xF9007C);
					ball.alpha = 0.5;
					box.addChild(ball);
			target = box;
			
			addEventListener(Event.ENTER_FRAME,frameLogic)
			isAdd = true;
		}
		
		private function frameLogic(e:Event):void 
		{
			//target.x += 1;
			//target.y += 1;
			//target.z+=1
			//target.y+=0.2;
		}
	}
}